package de.tum.in.elitese.common;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.swing.JOptionPane;

import sun.misc.BASE64Encoder;

/**
 * Helper Class for all digests.
 * 
 * @author Christoph Frenzel
 * 
 */
public class DigestHelper {
	private static final DigestHelper INSTANCE = new DigestHelper();

	public static DigestHelper getInstance() {
		return INSTANCE;
	}

	private BASE64Encoder fEncoder;
	private MessageDigest fMessageDigest;

	private DigestHelper() {
		try {
			fMessageDigest = MessageDigest.getInstance("SHA-256");
		} catch (NoSuchAlgorithmException e) {
			JOptionPane.showMessageDialog(null, "Security Error.",
					"SHA-256 could not be found.", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
			System.exit(-1);
		}

		fEncoder = new BASE64Encoder();
	}

	public String getDigestForString(String string) {
		byte[] bytes = fMessageDigest.digest(string.getBytes());

		return fEncoder.encode(bytes);
	}
}
